package io.gitee.bluemiaomiao.dataanalyzebackend.util;

import io.gitee.bluemiaomiao.dataanalyzebackend.api.DefaultResultimpl;
import io.gitee.bluemiaomiao.dataanalyzebackend.api.ResultCode;
import lombok.Getter;

/**
 * @author ：TLang
 * @date ：Created in 2020/10/15 11:20
 * @description：通用返回对象
 * @modified By：
 * @version: 1.0$
 */
@Getter
public class ResultCodeUtil<T> {
  private long code;
  private String message;
  private T data;

  protected ResultCodeUtil() {}

  /**
   * author: TLang description:带参构造函数
   *
   * @params long code,String message,T data
   * @return null
   * @create time: 2020/10/15 11:26
   */
  private ResultCodeUtil(long code, String message, T data) {
    this.code = code;
    this.message = message;
    this.data = data;
  }

  /**
   * @author: TLang
   * @description: 成功返回结果
   * @params data 获取数据
   * @return CommonResult
   * @create time: 2020/10/15 11:27
   */
  public static <T> ResultCodeUtil<T> success(T data) {
    return new ResultCodeUtil<T>(
        DefaultResultimpl.SUCCESS.getCode(), DefaultResultimpl.SUCCESS.getMessage(), data);
  }

  /**
   * @author: TLang
   * @description: 成功返回结果
   * @param data 获取的数据
   * @param message 提示信息
   * @return CommonResult
   * @create time: 2020/10/15 11:33
   */
  public static <T> ResultCodeUtil<T> success(T data, String message) {
    return new ResultCodeUtil<T>(DefaultResultimpl.SUCCESS.getCode(), message, data);
  }

  /**
   * author: TLang description:失败返回结果
   *
   * @param errorCode 错误码
   * @return CommonResult
   * @create time: 2020/10/15 11:27
   */
  public static <T> ResultCodeUtil<T> failed(ResultCode errorCode) {
    return new ResultCodeUtil<T>(errorCode.getCode(), errorCode.getMessage(), null);
  }

  /**
   * @author: TLang
   * @description: 失败返回结果
   * @param errorCode 错误码
   * @param message 错误信息
   * @return CommonResult
   * @create time: 2020/10/15 11:27
   */
  public static <T> ResultCodeUtil<T> failed(ResultCode errorCode, String message) {
    return new ResultCodeUtil<T>(errorCode.getCode(), message, null);
  }

  /**
   * author: TLang description:失败返回结果
   *
   * @param null
   * @return failed
   * @create time: 2020/10/15 11:27
   */
  public static <T> ResultCodeUtil<T> failed() {
    return failed(DefaultResultimpl.FAILED);
  }

  /**
   * author: TLang description:参数验证失败返回结果
   *
   * @param null
   * @return failed
   * @create time: 2020/10/15 11:27
   */
  public static <T> ResultCodeUtil<T> validateFailed() {
    return failed(DefaultResultimpl.VALIDATE_FAILED);
  }

  /**
   * author: TLang description:参数验证失败返回结果
   *
   * @param message 提示信息
   * @return CommonResult
   * @create time: 2020/10/15 11:27
   */
  public static <T> ResultCodeUtil<T> validateFailed(String message) {
    return new ResultCodeUtil<T>(DefaultResultimpl.VALIDATE_FAILED.getCode(), message, null);
  }

  /**
   * author: TLang description:未登录返回结果
   *
   * @param data 返回数据
   * @return CommonResult
   * @create time: 2020/10/15 11:27
   */
  public static <T> ResultCodeUtil<T> unauthorized(T data) {
    return new ResultCodeUtil<T>(
        DefaultResultimpl.UNAUTHORIZED.getCode(),
        DefaultResultimpl.UNAUTHORIZED.getMessage(),
        data);
  }

  /**
   * author: TLang description:未授权返回结果
   *
   * @param data 返回数据
   * @return CommonResult
   * @create time: 2020/10/15 11:27
   */
  public static <T> ResultCodeUtil<T> forbidden(T data) {
    return new ResultCodeUtil<T>(
        DefaultResultimpl.FORBIDDEN.getCode(), DefaultResultimpl.FORBIDDEN.getMessage(), data);
  }

  @Override
  public String toString() {
    return super.toString();
  }
}
